package com.tengfei;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @version v1.0
 * @author} feige
 * @date 2021-10-24-22:29
 * @description
 */
public class JsonTest {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = doGet("http://localhost:8080/stu/find");
                ObjectMapper mapper = new ObjectMapper();
                try {
                    ResponseJson responseJson = mapper.readValue(result, ResponseJson.class);
                    System.out.println(responseJson);
                    JsonNode jsonNode = mapper.readTree(result);
                    JsonNode student = jsonNode.get("data");
                    ResponseJson.Student student2 = mapper.treeToValue(student, ResponseJson.Student.class);
                    System.out.println(student2);
                    Student student1 = mapper.treeToValue(student, Student.class);
                    System.out.println(student1);
//                    ResponseJson.Student student2 = mapper.convertValue(student, ResponseJson.Student.class);
//                    Map<String,ResponseJson.Student> map=mapper.convertValue(student, Map<String,ResponseJson.Student>.Class);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


    public static String doGet(String url) {
        HttpURLConnection urlConnection = null;
        InputStream inputStream = null;
        StringBuilder stringBuilder = new StringBuilder();
        URL requesturl = null;
        try {
            // 创建URL
            requesturl = new URL(url);
            // 获取UrlConnection对象
            urlConnection = (HttpURLConnection) requesturl.openConnection();
            // 配置超时时间,5秒钟
            urlConnection.setConnectTimeout(8000);
            urlConnection.setReadTimeout(8000);
            // 请求数据格式
            urlConnection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
            // 配置请求方式
            urlConnection.setRequestMethod("GET");
            // 发起请求
            urlConnection.connect();
            if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = urlConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader reader = new BufferedReader(inputStreamReader);
                String temp = "";
                while ((temp = reader.readLine()) != null) {
                    stringBuilder.append(temp);
                }
                reader.close();
                return stringBuilder.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }
}
